package com.slg.android.manyanime.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.slg.android.manyanime.R;
import com.slg.android.manyanime.bean.Collection;
import com.slg.android.manyanime.utils.DataStore;

import java.util.List;

/**
 * Created by Darker on 2015/3/17.
 */
public class CollectionAdapter extends BaseAdapter {
    private Context context;
    private List<Collection> data;

    public CollectionAdapter(Context context,List<Collection> data){
        this.context = context;
        this.data = data;
    }
    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Collection collection = data.get(position);
        if(convertView == null){
            convertView = LayoutInflater.from(context)
                    .inflate(R.layout.item_my_collection_animay, parent, false);
        }
        ViewHolder holder = (ViewHolder) convertView.getTag();
        if(holder == null){
            holder = new ViewHolder();
            convertView.setTag(holder);
        }
        ViewUtils.inject(holder,convertView);
//todo 收藏的图片
        holder = (ViewHolder)convertView.getTag();
        BitmapUtils bitmapUtils = DataStore.getBitmapUtils();
        bitmapUtils.configDefaultLoadFailedImage(R.drawable.cartoon_hot_loading);
        bitmapUtils.configDefaultLoadingImage(R.drawable.cartoon_hot_loading);
        bitmapUtils.display(holder.titlePic, collection.getPic());

        holder.colName.setText(collection.getName());
        if("1".equals(collection.getIsOver())){
            holder.colUpd.setText("已完结，共"+collection.getTotalNum()+"集");
        }else{
            holder.colUpd.setText("更新至"+collection.getLastUp()+"集");
        }

        return convertView;
    }

    public static class ViewHolder{
        @ViewInject(R.id.item_my_collection_picture)
        public ImageView titlePic;
        @ViewInject(R.id.item_my_collection_name)
        public TextView colName;
        @ViewInject(R.id.item_my_collection_update)
        public TextView colUpd;

    }
}
